import socketserver


class MyTCPHandle(socketserver.BaseRequestHandler):
    conn_list = []

    def setup(self):
        self.conn_list.append(self.request)

    def handle(self):
        while True:
            try:
                self.data= self.request.recv(1024)
                # 发送数据
                self.send_data(self.conn_list, self.data)
                # self.request.send(self.data.upper())
            except Exception as e:
                print(self.client_address, e)
                self.conn_list.remove(self.request)
                break

    def send_data(self, conns, data):
        for conn in conns:
            conn.send(data)

if __name__ == '__main__':
    host, port = 'localhost', 9999
    server = socketserver.ThreadingTCPServer((host, port), MyTCPHandle)
    server.serve_forever()